#include "../Include/CountDownLatch.h"

CCountDownLatch::CCountDownLatch(Int32 nCount)
	:m_mutex(),
	m_cond(m_mutex),
	m_nCount(nCount)
{
}

void CCountDownLatch::Wait()
{
	CMutexLocker locker(&m_mutex);
	while (m_nCount > 0)
	{
		m_cond.Wait();
	}
}

void CCountDownLatch::CountDown()
{
	CMutexLocker locker(&m_mutex);
	--m_nCount;
	if (m_nCount == 0)
	{
		m_cond.NotifyAll();
	}
}

int CCountDownLatch::GetCount() const
{
	CMutexLocker locker(&m_mutex);
	return m_nCount;
}

void CCountDownLatch::CountDown_(void *arg)
{
	CCountDownLatch *pLatch = (CCountDownLatch*)arg;
	pLatch->CountDown();
}

